package category

import (
	"context"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/golang/protobuf/ptypes/empty"
	"go.uber.org/zap"
	"net/http"
	"shop_webs/goods_web/api"
	"shop_webs/goods_web/forms"
	"shop_webs/goods_web/global"
	"shop_webs/goods_web/proto"
	"strconv"
)

func List(ctx *gin.Context) {
	rsp, err := global.GoodsSrvClient.GetAllCategoryList(context.Background(), &empty.Empty{})
	if err != nil {
		zap.S().Errorw("[List] 查询 【商品分类列表】失败", "msg", err.Error())
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	data := make([]interface{}, 0)
	if err := json.Unmarshal([]byte(rsp.JsonData), &data); err != nil {
		zap.S().Errorw("[List] 查询 【商品分类列表】失败", "msg", err.Error())
		ctx.Status(http.StatusInternalServerError)
		return
	}
	ctx.JSON(http.StatusOK, data)
}
func New(ctx *gin.Context) {
	categoryForm := forms.CategoryForm{}
	if err := ctx.ShouldBindJSON(&categoryForm); err != nil {
		api.HandleValidatorError(err, ctx)
		return
	}
	rsp, err := global.GoodsSrvClient.CreateCategory(context.Background(), &proto.CategoryInfoRequest{
		Name:           categoryForm.Name,
		IsTab:          *categoryForm.IsTab,
		Level:          categoryForm.Level,
		ParentCategory: categoryForm.ParentCategory,
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	reMap := make(map[string]interface{})
	reMap["id"] = rsp.Id
	reMap["name"] = rsp.Name
	reMap["level"] = rsp.Level
	reMap["parent"] = rsp.ParentCategory
	reMap["is_tab"] = rsp.IsTab
	ctx.JSON(http.StatusOK, reMap)
}
func Detail(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}

	reMap := make(map[string]interface{})
	subCategorys := make([]interface{}, 0)
	if r, err := global.GoodsSrvClient.GetSubCategory(context.Background(),
		&proto.CategoryListRequest{Id: int32(i)}); err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)

	} else {
		for _, category := range r.SubCategorys {
			subCategorys = append(subCategorys, map[string]interface{}{
				"id":              category.Id,
				"name":            category.Name,
				"level":           category.Level,
				"parent_category": category.ParentCategory,
				"is_tab":          category.IsTab,
			})
		}
		reMap["id"] = r.Info.Id
		reMap["name"] = r.Info.Name
		reMap["level"] = r.Info.Level
		reMap["parent_category"] = r.Info.ParentCategory
		reMap["is_tab"] = r.Info.IsTab
		reMap["sub_categorys"] = subCategorys
		ctx.JSON(http.StatusOK, reMap)
	}
	return
}
func Delete(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}

	//1.先查询出该分类下的所有子分类
	//2. 将所有的分类全部逻辑删除
	//3. 将该分类下的所有的商品逻辑删除
	_, err = global.GoodsSrvClient.DeleteCategory(context.Background(), &proto.DeleteCategoryRequest{Id: int32(i)})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.Status(http.StatusOK)
}
func Update(ctx *gin.Context) {
	categoryForm := forms.UpdateCategoryForm{}
	if err := ctx.ShouldBindJSON(&categoryForm); err != nil {
		api.HandleValidatorError(err, ctx)
		return
	}

	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}

	request := &proto.CategoryInfoRequest{
		Id:   int32(i),
		Name: categoryForm.Name,
	}
	if categoryForm.IsTab != nil {
		request.IsTab = *categoryForm.IsTab
	}
	if _, err := global.GoodsSrvClient.UpdateCategory(context.Background(), request); err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": "更新成功",
	})
}
